﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsTest
{
    public partial class EditForm : Form
    {
        private int id;

        public EditForm()
        {
            InitializeComponent();
        }

        public EditForm(int id, string book, string name, DateTime createdTime)
        {
            InitializeComponent();
            this.id = id;
            textBox1.Text = book;
            textBox2.Text = name;
            textBox3.Text = createdTime.ToString();
        }

        private void EditForm_Load(object sender, EventArgs e)
        {
            BackColor = Color.Pink;
            this.CenterToScreen();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var createdTimeStr = textBox3.Text.ToString();
            var book = textBox1.Text;
            var name = textBox2.Text;
            var createdTime = createdTimeStr.Count() > 0 ? DateTime.Parse(createdTimeStr) : DateTime.Now;
            //修改
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("update Articles set Title='{0}',Author='{1}',CreatedTime='{2}'where Id={3}", book,name,createdTime, this.id);
                    var dt = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //增加
            else
            {
                if (!string.IsNullOrEmpty(name))
                {
                    var sql = string.Format("insert into Articles(Title,Author,CreatedTime)values('{0}','{1}','{2}')", book,name,createdTime);
                    var dt = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (dt == 1)
                    {
                        MessageBox.Show("添加成功", "提示");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
                else
                {
                    MessageBox.Show("标题和作者不能为空！", "提示");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
            this.Close();
         
        }
    }
}
